#! /usr/bin/env python

import os, re
from waf_dynamo import apidoc_extract_task

def configure(conf):
    pass

def build(bld):
    load_addrs = 'load_addrs_null.cpp'
    sig_handler = 'backtrace_null.cpp'
    file_handler = 'file_null.cpp'

    crashnull = bld.stlib(features = 'cxx',
                          includes = ['.', '../proto'],
                          proto_gen_py = True,
                          proto_gen_cc = True,
                          protoc_includes = '../proto',
                          source = 'crash_null.cpp script_crash_null.cpp ' + load_addrs + ' ' + sig_handler + ' ' + file_handler,
                          target = 'crashext_null')

    if 'android' in bld.env['PLATFORM']:
        sig_handler = 'backtrace_libunwind_ndk.cpp'
        load_addrs = 'load_addrs_proc_smap.cpp'
        file_handler = 'file_posix.cpp'
    elif 'linux' in bld.env['PLATFORM']:
        sig_handler = 'backtrace_execinfo.cpp'
        load_addrs = 'load_addrs_proc_smap.cpp'
        file_handler = 'file_posix.cpp'
    elif not ('web' in bld.env['PLATFORM'] or 'win32' in bld.env['PLATFORM']):
        file_handler = 'file_posix.cpp'
        sig_handler = 'backtrace_execinfo.cpp'

    if 'web' in bld.env['PLATFORM']:
        sig_handler = 'backtrace_jsweb.cpp';
        file_handler = 'file_posix.cpp';

    if 'macos' in bld.env['PLATFORM'] or 'ios' in bld.env['PLATFORM']:
        load_addrs = 'load_addrs_mach.cpp'

    if bld.env['PLATFORM'] in ['x86_64-macos']:
        sig_handler = 'backtrace_libunwind.cpp'

    if 'win32' in bld.env['PLATFORM']:
        load_addrs = 'load_addrs_win32.cpp'
        sig_handler = 'backtrace_win32.cpp'
        file_handler = 'file_win32.cpp'

    if bld.env['PLATFORM'] in ('arm64-nx64','x86_64-ps4','x86_64-ps5'):
        load_addrs = 'load_addrs_null.cpp'
        sig_handler = 'backtrace_null.cpp'
        file_handler = 'file_null.cpp'

    crash = bld.stlib(features = 'cxx',
                      includes = ['.', '../proto'],
                      proto_gen_py = True,
                      proto_gen_cc = True,
                      protoc_includes = '../proto',
                      source = 'crash.cpp script_crash.cpp ' + load_addrs + ' ' + sig_handler + ' ' + file_handler,
                      target = 'crashext')

    bld.recurse('test')
    bld.recurse('tool')
    bld.install_files('${PREFIX}/include/crash', 'crash.h')

    apidoc_extract_task(bld, ['script_crash.cpp'])


